﻿// -----------------------------------------------------------------------
// <copyright file="GoFiguration.Services.cs" company="Eric Swanson">
// 
// SUMMARY: *Open source and free to use and modify! 
//          (including Commercially)
// 
// Copyright (c) 2012 Eric Swanson
// 
// Permission is hereby granted, free of charge, to any person obtaining 
// a copy of this software and associated documentation files (the 
// "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, 
// distribute, sublicense, and/or sell copies of the Software, and to 
// permit persons to whom the Software is furnished to do so, subject to 
// the following conditions:
// 
// The above copyright notice and this permission notice shall be 
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
// SOFTWARE.
// </copyright>
// -----------------------------------------------------------------------

namespace GoFiguration.Services
{
	using System.IO;
	using System.Runtime.Serialization.Json;
	using System.ServiceModel;
	using System.ServiceModel.Activation;
	using System.ServiceModel.Web;
	using System.Xml.Serialization;

	/// <summary>
	/// The GoFigure WCF service.
	/// </summary>
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	[ServiceBehavior(Namespace = "http://gofigure.codeplex.com", InstanceContextMode = InstanceContextMode.PerCall)]
	[ServiceContract]
	public class GoFigureService
	{
		/// <summary>
		/// Gets the configuration setting value as XML using the specified <paramref name="key"/>.
		/// </summary>
		/// <param name="key">The setting key.</param>
		/// <returns>The configuration setting value as an XML <see cref="System.String"/>.</returns>
		//[OperationContract]
		[WebInvoke(UriTemplate = "", Method = "POST")]
		public string GetAsXML(string key)
		{
			if (GoFigure.ContainsKey(key))
			{
				var value = GoFigure.Get<object>(key, null);
				var stream = new MemoryStream();
				var serializer = new XmlSerializer(value.GetType());
				serializer.Serialize(stream, value);
				stream.Position = 0;
				var reader = new StreamReader(stream);
				var result = reader.ReadToEnd();
				return result;
			}

			return null;
		}

		/// <summary>
		/// Gets the configuration setting value as a JSON <see cref="System.String"/> using the specified <paramref name="key"/>.
		/// </summary>
		/// <param name="key">The setting key as JSON.</param>
		/// <returns>The configuration setting value as a JSON <see cref="System.String"/>.</returns>
		//[OperationContract]
		[WebGet(UriTemplate = "{*key}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
		public string GetAsJSON(string key)
		{
			if (GoFigure.ContainsKey(key))
			{
				var value = GoFigure.Get<object>(key, null);
				var stream = new MemoryStream();
				var serializer = new DataContractJsonSerializer(value.GetType());
				serializer.WriteObject(stream, value);
				stream.Position = 0;
				var reader = new StreamReader(stream);
				var result = reader.ReadToEnd();
				return result;
			}

			return null;
		}
	}
}